package com.tju.serviceOrder.controller;


import com.tju.Result;
import com.tju.serviceOrder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2022-01-18
 */
@RestController
@CrossOrigin
@RequestMapping("/serviceOrder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    //生成微信支付二维码  参数是订单号
    @GetMapping("createNative/{orderNo}")
    public Result createNative(@PathVariable String orderNo){
        Map<String,Object> map = payLogService.createNative(orderNo);
        System.out.println("===返回二维码map集合" + map);
        return Result.ok().data(map);
    }

    //查询订单支付状态信息
    @GetMapping("queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo){
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("====查询订单状态map集合" + map);
        if (map == null){
            return Result.error().message("支付出错了!");
        }
        if (map.get("trade_state").equals("SUCCESS")){
            //支付成功，添加记录到支付表格
            payLogService.updateOrderState(map);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中").code(25000);
    }

}

